﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Drawing; 
using System.Drawing.Drawing2D;


namespace SelfMonitor
{
    public partial class FMain : Form
    {
        public string RootPath { get; set; }
        public string TaskPath { get; set; }
        public FMain(string RP)
        {
            InitializeComponent();
            RootPath = RP;
        }

        private void designToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Wizard fw = new Wizard("Design Project Wizard", RootPath);
            fw.RV += new Wizard.ReturnValue(fw_ReturnValue);
            fw.ShowDialog();
            FAddTasks fat = new FAddTasks(RootPath);
            fat.Show();
        }

        

        private void programmingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Wizard fw = new Wizard("Programming Project Wizard", RootPath);
            fw.RV += new Wizard.ReturnValue(fw_ReturnValue);
            fw.ShowDialog();
            FAddTasks fat = new FAddTasks(RootPath);
            fat.Show();
        }

        private void recitingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Wizard fw = new Wizard("Reciting Project Wizard",RootPath);
            fw.RV += new Wizard.ReturnValue(fw_ReturnValue);
            fw.ShowDialog();
            FAddTasks fat = new FAddTasks(RootPath);
            fat.Show();
        }


        private void FMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        //******************************** My Own Functions *******************************//
        private void fw_ReturnValue(object sender, ReturnEvent e)
        {
            DirectoryInfo RootFolder = new DirectoryInfo(RootPath);
            foreach (DirectoryInfo NextFolder in RootFolder.GetDirectories())
               if(Goals.Items.Contains(NextFolder.Name))
                    continue;
                else
                    Goals.Items.Add(NextFolder.Name);
           
           
        }
        //*******************************************************************************//


        private void FMain_Load(object sender, EventArgs e)
        {
            DirectoryInfo RootFolder = new DirectoryInfo(RootPath);
            foreach (DirectoryInfo NextFolder in RootFolder.GetDirectories())
                if(Goals.Items.Contains(NextFolder.Name))
                    continue;
                else
                    Goals.Items.Add(NextFolder.Name);
            button3.Enabled = false;
            button4.Enabled = false;
        }

        private void ReadProjectInfo(string Path)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(Path);
            XmlNodeList nodes = xd.SelectSingleNode("Project/ProjectInfo").ChildNodes;
            foreach (XmlNode xn in nodes)
                switch (xn.Name)
                {
                    case "ProjectClass":
                        //label1.Text =xn.InnerText;
                        break;
                    case "ProjectName":
                        label3.Text = xn.InnerText;
                        break;
                    case "TaskNumbers":
                        label9.Text = xn.InnerText;
                        break;
                    case "Percent":
                        progressBar1.Value = Convert.ToInt32(xn.InnerText);
                        break;
                    case "StartDate":
                        label5.Text = xn.InnerText;
                        break;
                    case "EndDate":
                        label6.Text = xn.InnerText;
                        break;
                    default:
                        MessageBox.Show(xn.Name);
                        break;
                }
                  
        }


        //Generate new image dimensions
        public Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
        {
            //double to hold the final multiplier to use when scaling the image
            double multiplier = 0;

            //string for holding layout
            string layout;

            //determine if it's Portrait or Landscape
            if (currH > currW) layout = "portrait";
            else layout = "landscape";

            switch (layout.ToLower())
            {
                case "portrait":
                    //calculate multiplier on heights
                    if (destH > destW)
                    {
                        multiplier = (double)destW / (double)currW;
                    }

                    else
                    {
                        multiplier = (double)destH / (double)currH;
                    }
                    break;
                case "landscape":
                    //calculate multiplier on widths
                    if (destH > destW)
                    {
                        multiplier = (double)destW / (double)currW;
                    }

                    else
                    {
                        multiplier = (double)destH / (double)currH;
                    }
                    break;
            }

            //return the new image dimensions
            return new Size((int)(currW * multiplier), (int)(currH * multiplier));
        }

        //Resize the image
        private void SetImage(PictureBox pb)
        {
            try
            {
                //create a temp image
                Image img = pb.Image;

                //calculate the size of the image
                Size imgSize = GenerateImageDimensions(img.Width, img.Height, this.pictureBox1.Width, this.pictureBox1.Height);

                //create a new Bitmap with the proper dimensions
                Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);

                //create a new Graphics object from the image
                Graphics gfx = Graphics.FromImage(img);

                //clean up the image (take care of any image loss from resizing)
                gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //empty the PictureBox
                pb.Image = null;

                //center the new image
                pb.SizeMode = PictureBoxSizeMode.CenterImage;

                //set the new image
                pb.Image = finalImg;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }


        private String ReadTask(string path, int TaskNo)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(path);
            XmlNodeList nodes = xd.SelectSingleNode("Project").ChildNodes;
            XmlNode xn = nodes.Item(1);
            XmlNodeList subnodes = xn.ChildNodes;
            String Taskpath = "";

            
            foreach (XmlNode s in subnodes)
            {
                switch (s.Name)
                {
                    case "Name":
                        //MessageBox.Show(s.InnerText);
                        break;
                    case "Group":
                        //MessageBox.Show(s.InnerText);
                        break;
                    case "Type":
                        //MessageBox.Show(s.InnerText);
                        break;
                    case "SourceFile":
                        Taskpath = s.InnerText;
                       DirectoryInfo di = new System.IO.DirectoryInfo(s.InnerText);
                        foreach (FileInfo file in di.GetFiles())
                        {
                           // MessageBox.Show(file.FullName);
                            if (file.Name.Contains("Start"))
                            {
                                pictureBox1.Image = Image.FromFile(file.FullName);
                                label10.Text = file.Name;
                            }
                            //SetImage(pictureBox1);

                        }
                        break;
                    case "Instruction":
                        StreamReader sr = new StreamReader(s.InnerText,Encoding.UTF8);
                        string instruction = sr.ReadToEnd();
                        textBox1.Text = instruction;
                        break;
                    default:
                        break;
                }

            }
        return Taskpath;
        }
        private void Goals_SelectedValueChanged(object sender, EventArgs e)
        {

            foreach (string item in Goals.Items)
                if (Goals.GetSelected(Goals.Items.IndexOf(item)))
                {   string XmlPath = System.IO.Path.Combine(RootPath,item,item+".xml");
                    ReadProjectInfo(XmlPath);
                    TaskPath = ReadTask(XmlPath, 0);
                    MessageBox.Show(TaskPath);
                    button3.Enabled = true;
                    button4.Enabled = true;
                   
                }
        }

        private void addTaskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FAddTasks fat = new FAddTasks(RootPath);
            fat.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(TaskPath + "\\FileList.txt",Encoding.UTF8);
            String[] files = sr.ReadToEnd().Split('\n');
            if (label10.Text.Contains(files[0].Trim()))
            {
                MessageBox.Show("Sorry,Already frontmost");
            }
            else
            {
                for (int i = 1; i <= files.Length - 1; i++)
                {
                    if (label10.Text.Contains(files[i].Trim()))
                    {
                        pictureBox1.Image = Image.FromFile(System.IO.Path.Combine(TaskPath, files[i - 1].Trim()));
                        label10.Text = files[i - 1].Trim();
                        return;
                    }

                }
            }

 
        }

        private void button4_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(TaskPath + "\\FileList.txt", Encoding.UTF8);
            String[] files = sr.ReadToEnd().Split('\n');
            if (label10.Text.Contains(files[files.Length-1].Trim()))
            {
                MessageBox.Show("Sorry,Already rearmost");
            }
            else
            {
                for (int i = 0; i < files.Length-1; i++)
                {
                    if (label10.Text.Contains(files[i].Trim()))
                    {
                        pictureBox1.Image = Image.FromFile(System.IO.Path.Combine(TaskPath, files[i + 1].Trim()));
                        label10.Text = files[i + 1].Trim();
                        return;
                    }

                }
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Program Files\\Adobe\\Adobe Photoshop CS6 (64 Bit)\\Photoshop.exe");
            //startInfo.WindowStyle = ProcessWindowStyle.Minimized;

            Process.Start(startInfo);
        }

        //*********************************************************************************//
    }
}
